package com.xindacloud.sms.adapter.controller;

import com.xindacloud.sms.adapter.dto.SmsReportDTO;
import com.xindacloud.sms.adapter.entity.SmsDownloadReportLog;
import com.xindacloud.sms.adapter.service.business.ISmsDeliveryReceiptService;
import com.xindacloud.sms.adapter.service.business.ISmsDownloadReportLogService;
import com.xindacloud.sms.adapter.utils.StatusEum;
import com.xindacloud.sms.adapter.vo.SmsReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Objects;
import java.util.stream.Collectors;

@Api(tags = {"查询短信报告接口模块"})
@Slf4j
@RestController
@RequestMapping(value = "/api/v1/adapter/sms/report")
public class ReportController {

    final ISmsDeliveryReceiptService deliveryReceiptService;

    final ISmsDownloadReportLogService downloadReportLogService;

    @Autowired
    public ReportController(ISmsDeliveryReceiptService deliveryReceiptService, ISmsDownloadReportLogService downloadReportLogService) {
        this.deliveryReceiptService = deliveryReceiptService;
        this.downloadReportLogService = downloadReportLogService;
    }

    @ApiOperation(value = "查询短信报告", response = SmsReportVO.class)
    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object queryReport(@ApiParam(name = "smsReportDTO", value = "查询短信报告DTO", required = true)
                              @RequestBody SmsReportDTO smsReportDTO,
                              @ApiParam(name = "username", value = "用户名", required = true)
                              @RequestHeader(name = "username") String username,
                              @ApiParam(name = "password", value = "密码", required = true)
                              @RequestHeader(name = "password") String password) {

        final SmsDownloadReportLog smsDownloadReportLog = downloadReportLogService.saveSmsDownloadReportLog(smsReportDTO, username, password);

        String status = "";
        try {

            status = StatusEum.SUCCESS.desc;
            return deliveryReceiptService.querySmsReportFor(smsReportDTO, username, password);
                    /*.stream()
                    .peek(rs -> {
                        final String reason = Objects.nonNull(rs.getCustomReason()) ? rs.getCustomReason() : rs.getReason();
                        rs.setReason(reason);
                    }).collect(Collectors.toList());*/

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            status = StatusEum.FAILED.desc;
        } finally {
            downloadReportLogService.updateSmsDownloadReportLog(SmsDownloadReportLog.builder()
                    .logId(smsDownloadReportLog.getLogId())
                    .downloadStatus(status)
                    .build());
        }

        return Collections.emptyList();
    }

    @ApiOperation(value = "短信报告日志", response = SmsReportVO.class)
    @PostMapping(value = "/logs", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object queryReportLog(@ApiParam(name = "username", value = "用户名", required = true)
                                 @RequestHeader(name = "username") String username,
                                 @ApiParam(name = "password", value = "密码", required = true)
                                 @RequestHeader(name = "password") String password) {
        return downloadReportLogService.querySmsDownloadReportLog(username, password);
    }

}
